package yxk.report.esc.hssc;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.mcontrol.mobtable.MobTablePackageDataHandler;
import kd.bos.form.mcontrol.mobtable.MobTableRowBuilder;
import kd.bos.form.mcontrol.mobtable.MobTableRowData;
import kd.bos.form.mcontrol.mobtable.events.MobTableHandleResult;
import kd.bos.form.mcontrol.mobtable.events.MobTablePackageDataHandlerArgs;
import kd.bos.form.mcontrol.mobtable.tablecolumn.MobTableColumn;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.*;

public class MyMobTablePackageDataHandler extends MobTablePackageDataHandler {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

    private Map<String, Map<String, Map<String, String>>> calamountMap;
    private String bles;

    public MyMobTablePackageDataHandler() {

    }

    public MyMobTablePackageDataHandler(Map map, String bes) {
        this.calamountMap = map;
        this.bles = bes;
    }

    @Override
    public MobTableHandleResult handleData(MobTablePackageDataHandlerArgs args) {
        Calendar instance = Calendar.getInstance();
        List<MobTableColumn> mobTableColumns = args.getMobTableColumns();
        MobTableHandleResult mobTableHandleResult = new MobTableHandleResult();
        List<MobTableRowData> mobTableRowDataList = new ArrayList<>();
        int index = 0;
        if ("1".equals(bles)) {
            for (String row : calamountMap.keySet()) {
                Map<String, Map<String, String>> monthMap = calamountMap.get(row);
                MobTableRowData mobTableRowData = MobTableRowBuilder.buildTemplateRowData(index, mobTableColumns);
                DynamicObject single = BusinessDataServiceHelper.loadSingleFromCache("haos_adminorghr", "name,number,id", new QFilter("number", QCP.equals, row).toArray());
                mobTableRowData.setValue("nwg5_orgname", single.getString("name"));
                for (String monthRow : monthMap.keySet()) {
                    try {
                        Map<String, String> bigRow = monthMap.get(monthRow);
                        instance.setTime(format.parse(monthRow));
                        int month = instance.get(Calendar.MONTH);
                        String calamount = bigRow.get("calamount");

                        switch (1 + month) {
                            case 1:
                                mobTableRowData.setValue("nwg5_january", calamount);
                                break;
                            case 2:
                                mobTableRowData.setValue("nwg5_february", calamount);
                                break;
                            case 3:
                                mobTableRowData.setValue("nwg5_march", calamount);
                                break;
                            case 4:
                                mobTableRowData.setValue("nwg5_april", calamount);
                                break;
                            case 5:
                                mobTableRowData.setValue("nwg5_may", calamount);
                                break;
                            case 6:
                                mobTableRowData.setValue("nwg5_june", calamount);
                                break;
                            case 7:
                                mobTableRowData.setValue("nwg5_july", calamount);
                                break;
                            case 8:
                                mobTableRowData.setValue("nwg5_august", calamount);
                                break;
                            case 9:
                                mobTableRowData.setValue("nwg5_september", calamount);
                                break;
                            case 10:
                                mobTableRowData.setValue("nwg5_october", calamount);
                                break;
                            case 11:
                                mobTableRowData.setValue("nwg5_november", calamount);
                                break;
                            case 12:
                                mobTableRowData.setValue("nwg5_december", calamount);
                                break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                mobTableRowDataList.add(mobTableRowData);
                index++;
            }
        } else if ("0".equals(bles)) {
            for (String row : calamountMap.keySet()) {
                Map<String, Map<String, String>> monthMap = calamountMap.get(row);
                MobTableRowData mobTableRowData = MobTableRowBuilder.buildTemplateRowData(index, mobTableColumns);
                DynamicObject single = BusinessDataServiceHelper.loadSingleFromCache("hsas_salaryfile", "empnumber,id",
                        new QFilter("employee.empnumber", QCP.equals, row).toArray());
                mobTableRowData.setValue("nwg5_name", single.getString("name"));
                for (String monthRow : monthMap.keySet()) {
                    try {
                        Map<String, String> bigRow = monthMap.get(monthRow);
                        instance.setTime(format.parse(monthRow));
                        int month = instance.get(Calendar.MONTH);
                        String  salary = bigRow.get("mount");//净薪资
                        String  mount = bigRow.get("salary");//总薪资

                        switch (1 + month) {
                            case 1:
                                mobTableRowData.setValue("january_salary", salary);
                                mobTableRowData.setValue("january_mount", mount);
                                break;
                            case 2:
                                mobTableRowData.setValue("february_salary", salary);
                                mobTableRowData.setValue("february_mount", mount);
                                break;
                            case 3:
                                mobTableRowData.setValue("march_salary", salary);
                                mobTableRowData.setValue("march_mount", mount);
                                break;
                            case 4:
                                mobTableRowData.setValue("april_salary", salary);
                                mobTableRowData.setValue("april_mount", mount);
                                break;
                            case 5:
                                mobTableRowData.setValue("may_salary", salary);
                                mobTableRowData.setValue("may_mount", mount);
                                break;
                            case 6:
                                mobTableRowData.setValue("june_salary", salary);
                                mobTableRowData.setValue("june_mount", mount);
                                break;
                            case 7:
                                mobTableRowData.setValue("july_salary", salary);
                                mobTableRowData.setValue("july_mount", mount);
                                break;
                            case 8:
                                mobTableRowData.setValue("august_salary", salary);
                                mobTableRowData.setValue("august_mount", mount);
                                break;
                            case 9:
                                mobTableRowData.setValue("september_salary", salary);
                                mobTableRowData.setValue("september_mount", mount);
                                break;
                            case 10:
                                mobTableRowData.setValue("october_salary", salary);
                                mobTableRowData.setValue("october_mount", mount);
                                break;
                            case 11:
                                mobTableRowData.setValue("november_salary", salary);
                                mobTableRowData.setValue("november_mount", mount);
                                break;
                            case 12:
                                mobTableRowData.setValue("december_salary", salary);
                                mobTableRowData.setValue("december_mount", mount);
                                break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                mobTableRowDataList.add(mobTableRowData);
                index++;
            }
        }
        mobTableHandleResult.setMobTableRowDataList(mobTableRowDataList);
        // 额外格式化信息如日期、金额，支持重写
        // mobTableHandleResult.setFmtInfo(getFmtInfo(args));
        return mobTableHandleResult;
    }
}